<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Offer extends CI_Controller {   
    private $base_url_trang='';
    private $total_rows='';
    private $per_page=70;
    var $content = '';
    function  __construct(){
        parent::__construct();        
    }
    function curlip($url){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_URL,$url);
        $result = curl_exec($ch);  // grab URL and pass it to the variable.
        curl_close($ch);
        return $result;
    }
    function survey($offset=0){// offer banner
        if($this->session->userdata('logedin')){
            if(!is_numeric($offset)){$offset=0;}        
            $this->base_url_trang=base_url().'offer/survey/';
            $this->total_rows = $this->Home_model->get_number('offer');  
            $this->phantrang(); 
            //check country by ip           
            $ip =$this->input->ip_address();
            
           $url ="http://ip2country.sourceforge.net/ip2c.php?format=XML&ip=".$ip;
          $t= $this-> curlip($url);
         
            //$nguon =$this->curlip($url);  
            $xml=simplexml_load_file($url);
            @$country==$xml->country_code;
            
            
            //echo $details->country; // -> "US"
            //////
            
            $this->db->like('country', @$country); 
            $offer = $this->Home_model->get_data('offer',array('show'=>1),array($this->per_page,$offset),array('title','ASC'),'',array('id','title','img','description','point_rate'));     
            $this->content=$this->load->view('offerbanner',
                        array(
                            'offer'=>$offer
                        ),true);
            $this->hienthi();
        }else redirect(base_url().'user/login');
    }
    function wall(){
        if($this->session->userdata('logedin')){
            $this->content = $this->load->view('offer_wall',
                        array(
                            'net'=>$this->Home_model->get_data('network',array('show'=>1,'type'=>'Wall'),array(),array('order','DESC'),'',array('id','title','script'))
                        ),true);
            $this->hienthi();
        }else redirect(base_url().'user/login');
    }
    function content(){
        //giong net wall nhung chi khac type
        //rieng thang nay co 1 view index la content trong offer/view/content de hien thi script o header
        //$data['script']= $this->Home_model->get_data('network',array('show'=>1,'type'=>'Content'),array(),array('order','DESC'),'',array('header_script'));
        if($this->session->userdata('logedin')){
            @$data->content = $this->load->view('offer_wall',
                        array(
                            'net'=>$this->Home_model->get_data('network',array('show'=>1,'type'=>'Content'),array(),array('order','DESC'),'',array('id','title','script'))
                        ),true);
           
        $dt = $this->template->render($data);  
        $this->load->view('content',$dt); 
        }else redirect(base_url().'user/login');
    }
    function detail($id=0){
        // ad vao bang track ing lay tracking add vao subid postback se get subid tai bang trackking
        if($this->session->userdata('logedin')){
            if(!is_numeric($id)){$id=0;}
            $off= $this->Home_model->get_one('offer',array('id'=>$id)); 
            if(!empty($off)){
                $this->db->insert('tracklink',
                                    array(
                                    'tracklink'=>'no',
                                    'id_user'=>$this->session->userdata('user')->id,
                                    'id_offer'=>$off->id
                                    )
                                    );
                $idi=$this->db->insert_id();
                $track= dechex($idi);  
                $this->db->where('id',$idi);
                $this->db->update('tracklink',array('tracklink'=>$track));
                $this->load->view('detail',
                            array(
                                'url'=>$off->url.$off->subid.$track//$off->subid: vi du &subid=
                            ));
            }else{
                $this->content = 'Offer doesn\'t exist';
                $this->hienthi();
            }
                        
        }else redirect(base_url().'user/login');
    }
    function newoffer(){
        $this->load->view('offer/vmod_newoffer',
                    array(
                        'n_offer'=>$this->Home_model->get_data('offer',array('show'=>1),array(10),array('id','DESC'),'',array('id','title','img','point_rate'))
                    ));
    }  
    function test(){
        $this->db->where('groupid',2);
        $this->db->set('total', 'curent+1', FALSE);
        $this->db->update('users'); 
    }
    function hienthi(){  
        @$data->content = $this->content;
        $dt = $this->template->render($data);  
        $this->load->view('default/viewfull',$dt);        
       
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = $this->base_url_trang;
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 3;
        $config['num_links'] = 12;
        $config['first_link'] = '<<';
        $config['first_tag_open'] = '<li class="firt_pag">';//div cho chu <<
        $config['first_tag_close'] = '</li>';//div cho chu <<
        $config['last_link'] = '>>';
        $config['last_tag_open'] = '<li class="last_pag">';
        $config['last_tag_close'] = '</li>';
        //-------next-
        $config['next_link'] = 'next &gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        //------------preview
        $config['prev_link'] = '&lt; prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
       // ------------------cu?npage
        $config['cur_tag_open'] = '<li class="current">';
        $config['cur_tag_close'] = '</li>';
        //--so 
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        //-----
        $this->pagination->initialize($config);
    }
}